package Messaging;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import main.MainCanvas;

//This class is responsible letting the user pick a contact
//          (to whom the sms will be sent)
//The lists are cached so that the user has to allow the application
//to read data once per list.
public class ContactsSelector implements CommandListener
{
    Display display;
    Displayable parentDisplayable;
    
    List contactGroupsList;
    List contactNamesList;
    
    Command selectCommand;
    Command cancelCommand;
    
    public static Vector contactGroups;
    public static Hashtable storedContacts=new Hashtable();
    private static String lastSelectedGroup="";
    
    public static boolean showingNames=true;
    
    public ContactsSelector(Display d, Displayable parentDisplayable)
    {
        display=d;
        this.parentDisplayable=parentDisplayable;
        cancelCommand=new Command("Cancel",Command.CANCEL,0);
        selectCommand=new Command("Select",Command.OK,1);
    }
    
    public void selectContact()
    {
        selectContactGroup();
    }
    
    //Displays the List containing the contact groups, i.e phone,sim,...
    private void selectContactGroup()
    {
        try
        {
            contactGroupsList = new List("Contacts from", List.EXCLUSIVE);
            contactGroupsList.addCommand(cancelCommand);
            contactGroupsList.addCommand(selectCommand);
            contactGroupsList.setCommandListener(this);
            
            if (contactGroups==null)
            {
                contactGroups=new Vector();
                String[] groups = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
                for (int i=0;i<groups.length;i++)
                {
                    contactGroupsList.append(groups[i], null);
                    contactGroups.addElement(groups[i]);
                }
            }
            else
            {
                for (int i=0;i<contactGroups.size();i++)
                {
                    contactGroupsList.append(contactGroups.elementAt(i).toString(), null);
                }
            }
            display.setCurrent(contactGroupsList);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    //Displays the List containing the names of the contacts
    private void selectContact(String group)
    {
        lastSelectedGroup=group;
        try
        {
            contactNamesList = new List("Pick a contact", List.EXCLUSIVE);
            contactNamesList.addCommand(cancelCommand);
            contactNamesList.addCommand(selectCommand);
            contactNamesList.setCommandListener(this);
            
            if (storedContacts.get(group)==null)
            {
                ContactList phoneList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, group);
                Enumeration items = phoneList.items();
                Vector contactsToSave=new Vector();
                while (items.hasMoreElements())
                {
                      Contact contact = (Contact) items.nextElement();
                      contactNamesList.append(contact.getString(Contact.FORMATTED_NAME,0), null);
                      contactsToSave.addElement(new ContactData(contact.getString(Contact.FORMATTED_NAME,0),contact.getString(Contact.TEL,0)));
                }
                storedContacts.put(group, contactsToSave);
                display.setCurrent(contactNamesList);
            }
            else
            {
                Vector savedContacts=(Vector)storedContacts.get(group);
                for (int i=0;i<savedContacts.size();i++)
                {
                    Object cont=savedContacts.elementAt(i);
                    ContactData cd=(ContactData)cont;
                    contactNamesList.append(cd.name,null);
                    display.setCurrent(contactNamesList);
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d)
    {
        if(c==cancelCommand)
        {
            display.setCurrent(parentDisplayable);
        }
        else if (c==selectCommand)
        {
            if (d==contactGroupsList)
            {
                if (contactGroupsList.getSelectedIndex()!=-1)
                    selectContact(contactGroupsList.getString(contactGroupsList.getSelectedIndex()));
            }
            else if (d==contactNamesList)
            {
                int selectedContactIndex=contactNamesList.getSelectedIndex();
                Vector savedContacts=(Vector)storedContacts.get(lastSelectedGroup);
                Object cont=savedContacts.elementAt(selectedContactIndex);
                ContactData cd=(ContactData)cont;
                SMSSender sender=new SMSSender(cd.phoneNumber, MainCanvas.ciCanvas.currentCurrency.toString(), cd.name, display, d, parentDisplayable);
                sender.sendSMS();
            }
        }
    }
}